﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Mimic
{
    abstract class AConverter<T, U> : IValueConverter
    {
        abstract protected Converter<T, U> MyConverter { get; }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // Check the target type
            if (targetType != typeof(U))
                throw new InvalidOperationException(string.Format("The target must be a {0} !", typeof(U)));
            return MyConverter((T)value);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }

    class VisibilityConverter : AConverter<bool, Visibility>
    {
        protected override Converter<bool, Visibility> MyConverter { get { return value => value ? Visibility.Visible : Visibility.Hidden; } }
    }

    class RefreshLabelConverter : AConverter<bool?, string>
    {
        protected override Converter<bool?, string> MyConverter
        { get { return value => string.Format("  refresh {0}windows", value.HasValue && value.Value ? "all " : ""); } }
    }

    class ToolTipMsgConverter : AConverter<bool, object>
    {
        protected override Converter<bool, object> MyConverter { get { return value => value ? null : "Show the windows list"; } }
    }

    class TrueFalseIconConverter : AConverter<bool, ImageSource>
    {
        // HACK : I don't like this way to get Image... But I don't know (yet) a best way to achieve that ! ... I would enjoy some help...
        protected override Converter<bool, ImageSource> MyConverter
        { get { return (value) => BitmapFrame.Create(new Uri(string.Format(@"pack://application:,,/Resources/{0}.png", value ? "tick" : "cross"))); } }
    }
}
